package com.codeest.geeknews.ui.main.adapter;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.codeest.geeknews.R;
import com.codeest.geeknews.model.bean.LastfmAlbumBean;
import com.codeest.geeknews.model.bean.MusicInfo;
import com.codeest.geeknews.presenter.main.LastfmPresenter;
import com.codeest.geeknews.ui.main.fragment.LasfmMusicFragment;
import com.codeest.geeknews.util.VideoUtil;
import com.codeest.geeknews.widget.RotateableRoundImageView;

import java.util.List;

import wseemann.media.FFmpegMediaMetadataRetriever;

/**
 * Created by admin on 2017/8/18.
 */

public class LastfmMusicListAdapter extends RecyclerView.Adapter<LastfmMusicListAdapter.ViewHolder> {
    private List<MusicInfo> data;
    private LastfmPresenter mPresente;
    LasfmMusicFragment mFragment;

    public interface OnMusicInfoListener {
        void onImageLoaded(RecyclerView.ViewHolder holder, int position, String url);

        void onMetadata(RecyclerView.ViewHolder holder, int position, String album, String artist);
    }

    public LastfmMusicListAdapter(List<MusicInfo> data, LastfmPresenter presenter, LasfmMusicFragment musicFragment) {
        this.data = data;
        this.mPresente = presenter;
        this.mFragment = musicFragment;
        if (!(mFragment instanceof OnMusicInfoListener)) {
            throw new RuntimeException(mFragment.getClass().getName() + "must implements " + OnMusicInfoListener.class.getName());
        }
    }

    @Override
    public LastfmMusicListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View v = inflater.inflate(R.layout.item_music, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
//        holder.itemView.post(new Runnable() {
//            @Override
//            public void run() {
//                Glide.with(holder.itemView.getContext())
//                        .load(data.get(position).getUrl())
//                        .asBitmap()
//                        .skipMemoryCache(true)
//                        //.into(new SimpleTarget<Bitmap>(holder.image.getWidth() / 2, holder.image.getHeight() / 2) {
//                        .into(new SimpleTarget<Bitmap>(300, 300) {
//                            @Override
//                            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
////                                BitmapDrawable unselected = new BitmapDrawable(BitmapUtil.toGrayscale(resource));
////                                BitmapDrawable selected = new BitmapDrawable(resource);
////                                Drawable drawable = new RevealDrawable(unselected, selected, RevealDrawable.HORIZONTAL);
////                                holder.image.setImageDrawable(drawable);
//                                holder.image.setImageDrawable(new BitmapDrawable(resource));
//                            }
//                        });
//            }
//        });
        ((RotateableRoundImageView) holder.image).stopRotation();
        VideoUtil.getMetadata(data.get(position).getUrl(), new VideoUtil.Callback() {
            @Override
            public void onMetadata(final FFmpegMediaMetadataRetriever mmr) {
                // final Bitmap bitmap = mmr.getFrameAtTime();
                // holder.image.setImageBitmap(bitmap);
                final String album = mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_ALBUM);
                final String artist = mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_ARTIST);
                mFragment.onMetadata(holder, position, album, artist);
                holder.image.setTag(R.id.tag_title, album);
                holder.image.setTag(R.id.tag_artist, artist);
                mPresente.getAlbumInfo(artist, album, new LastfmPresenter.ViewEx() {
                    @Override
                    public void showContent(LastfmAlbumBean lastfmAlbumBean) {
                        try {
                            final String url = lastfmAlbumBean.getAlbum().getImage().get(2).getText();
                            Glide.with(mFragment)
                                    .load(url)
                                    .asBitmap()
                                    .skipMemoryCache(true)
                                    .into(new SimpleTarget<Bitmap>(300, 300) {
                                        @Override
                                        public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                            holder.image.setImageDrawable(new BitmapDrawable(resource));
                                            mFragment.onImageLoaded(holder, position, url);
                                        }
                                    });
                        } catch (NullPointerException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        });
    }


    @Override
    public int getItemCount() {
        return data.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView image;

        public ViewHolder(View itemView) {
            super(itemView);
            image = (ImageView) itemView.findViewById(R.id.image);
        }
    }
}
